<?php

App::uses('AppController', 'Controller');

/**
 * Languages Controller
 *
 * @property Language $Language
 */
class LanguagesController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->Language->recursive = 0;
        $this->set('languages', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->Language->exists($id)) {
            throw new NotFoundException('Invalid language','default',array('class'=>'alert alert-danger'));
        }
        $options = array('conditions' => array('Language.' . $this->Language->primaryKey => $id));
        $this->set('language', $this->Language->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->Language->create();
            if ($this->Language->save($this->request->data)) {
                $this->Session->setFlash('The new language has been saved','default',array('class'=>'alert alert-info'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('The new language could not be saved. Please, try again.','default',array('class'=>'alert alert-danger'));
            }
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->Language->exists($id)) {
            throw new NotFoundException('Invalid language','default',array('class'=>'alert alert-danger'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Language->save($this->request->data)) {
                $this->Session->setFlash('The language has been successfully edited','default',array('class'=>'alert alert-info'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('The language could not be edited. Please, try again.','default',array('class'=>'alert alert-danger'));
            }
        } else {
            $options = array('conditions' => array('Language.' . $this->Language->primaryKey => $id));
            $this->request->data = $this->Language->find('first', $options);
        }
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Language->id = $id;
        if (!$this->Language->exists()) {
            throw new NotFoundException('Invalid language','default',array('class'=>'alert alert-danger'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Language->delete()) {
            $this->Session->setFlash('Language deleted','default',array('class'=>'alert alert-info'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash('Language was not deleted','default',array('class'=>'alert alert-danger'));
        $this->redirect(array('action' => 'index'));
    }

    public function show() {
        if($this->Session->check('Language') != true){
        $this->Session->write($this->Language->findByCode('en'));
        $this->redirect($this->referer());
        }
        
       $language = $this->Language->find('all');
        
        if(isset($this->params['requested']))
            {
                return $language;
            }
        
        $this->set(compact('language'));
    }
    
    public function lang(){
        $this->Session->write($this->Language->findByCode($this->request->query['l']));
        $this->redirect($this->referer());
    }

    public function beforefilter(){
        $this->Auth->allow('show','lang');
    }
}
